package com.bjsxt.livegoods.feedback.service.service.impl;

import com.bjsxt.livegoods.dao.feedback.FeedbackDao;
import com.bjsxt.livegoods.dao.order.OrderDao;
import com.bjsxt.livegoods.entity.Feedback;
import com.bjsxt.livegoods.entity.Order;
import com.bjsxt.livegoods.feedback.service.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Service
public class FeedbackServiceImpl implements FeedbackService {
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private FeedbackDao feedbackDao;
    @Override
    public Map<String, Object> insertFeedback(String feedback, String orderId) {
        //主键查询订单对象
        Query query=new Query();
        query.addCriteria(Criteria.where("_id").is(orderId));
        Order order = orderDao.findOrders(query).get(0);
        //维护评论实体对象
        Feedback feedback1=new Feedback();
        feedback1.setComment(feedback);
        feedback1.setOrderId(orderId);
        feedback1.setProductId(order.getProductId());
        feedback1.setStar(new Random().nextInt(5));
        String tmp=order.getUserPhone();
        String prefix=tmp.substring(0,3);
        String suffix=tmp.substring(7,11);
        feedback1.setUsername(prefix+"****"+suffix);
        feedbackDao.insertFeedback(feedback1);

        //更新订单，变更订单评论状态为2
        Update update=Update.update("commentState","2");
        orderDao.updateOrder(query,update);
        //返回结果
        Map<String,Object> result=new HashMap<>();
        result.put("status",200);
        result.put("msg","评论成功");
        return result;
    }
}
